home *** CD-ROM | disk | FTP | other *** search
/ CGI How-To / CGI HOW-TO.iso / chap2 / 2_5 / count.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-15  |  1.0 KB  |  49 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. main()
  5. {
  6.     char *script_name = getenv("SCRIPT_NAME");
  7.     char *path_info = getenv("PATH_INFO");
  8.     int count;
  9.  
  10.     printf("Content-type: text/html\n\n");
  11.  
  12.     printf("<HTML>\n");
  13.     printf("<HEAD><TITLE>CGI Script How-to: Test Form</TITLE></HEAD>\n");
  14.     printf("<BODY>\n");
  15.  
  16.     printf("<H1>CGI Script How-to<BR>determine the path of the script being executed</H1>\n");
  17.  
  18.     if (path_info == NULL || strcmp(path_info, "") == 0)
  19.     {
  20.         printf("<H2>This is the first time you called this script</H2><P>\n");
  21.         count = 1;
  22.     }
  23.     else
  24.     {
  25.         /* Offset the path by one character to skip the leading slash (/) */
  26.         count = atoi(path_info + 1);
  27.         count++;        /* increment the counter */
  28.         printf("<H2>You have called this script %d times</H2><P>\n", count);
  29.     }
  30.  
  31.     if (script_name != NULL)
  32.     {
  33.         printf("<A HREF=\"%s/%d\">Click here to call the script again</A>\n",
  34.             script_name, count);
  35.     }
  36.     else
  37.     { 
  38.         /* We can't link to the script so say it */
  39.         printf("I don't know who to call\n");
  40.     }
  41.  
  42.     printf("</BODY></HTML>\n");
  43.     exit(0);
  44. }
  45.  
  46. /*
  47.  * end of count.c
  48.  */
  49.